cd "PATH/TO/DATASET" // Change this

// Working version: Stata 16.1

capture log close

log using "temp_log", replace
import delimited "IMMI.csv", clear

set varabbrev off

// Generate DVs

gen ds1 = .
replace ds1 = ds1_sk1 if (skill == 1)
replace ds1 = ds1_sk2 if (skill == 2)
gen ds3 = .
replace ds3 = ds3_sk1 if (skill == 1)
replace ds3 = ds3_sk2 if (skill == 2)

gen t_skilled = skill - 1
gen t_foreign = 1 - home

gen supp = .
replace supp = ds1 if (t_foreign == 1)
replace supp = ds3 if (t_foreign == 0)

gen male = 2-sex

gen edu = .
replace edu = edu1 if (edu2 == 2)
replace edu = edu1-1 if (edu2 != 2)
replace edu = 1 if (edu == 0)
replace edu = . if (missing(edu2) | missing(edu1))

gen education = .
replace education = -2 if (edu < 4)
replace education = -1 if (edu == 4)
replace education = 0 if (edu == 5)
replace education = 1 if (edu >= 6)
replace education = . if missing(edu)

gen HsDropout = (education == -2)
replace HsDropout = . if missing(edu)
gen HsGrad = (education == -1)
replace HsGrad = . if missing(edu)
gen CollegeGrad = (education == 0)
replace CollegeGrad = . if missing(edu)
gen BAorAbove = (education == 1)
replace BAorAbove = . if missing(edu)
gen reg_education = education + 3

gen decrease = .
replace decrease = 1 if (supp < 4)
replace decrease = 0 if (supp >= 4)

** manuscript **

   // Figure 1
tab decrease t_skilled, col
ttest supp, by(t_skilled)

   // Figure 2
tab decrease t_foreign, col
ttest supp, by(t_foreign)

   // Figure 3
ttest supp if (t_foreign == 1), by(t_skilled) 
ttest supp if (t_foreign == 0), by(t_skilled)
ttest supp if (t_skilled == 0), by(t_foreign)
ttest supp if (t_skilled == 1), by(t_foreign)
      
** table **

	// Table 1
oprobit supp i.t_foreign i.t_skilled
oprobit supp i.t_foreign i.t_skilled age male
oprobit supp i.t_foreign##i.t_skilled age male
oprobit supp i.t_foreign i.t_skilled c.reg_education i.t_skilled#c.reg_education age male
oprobit supp i.t_foreign i.t_skilled i.HsDropout i.t_skilled#i.HsDropout i.HsGrad i.t_skilled#i.HsGrad i.BAorAbove i.t_skilled#i.BAorAbove age male

export delimited "IMMI_cleaned.csv"

log close
translate "temp_log.smcl" "Stata output.pdf", replace
rm temp_log.smcl
